package com.jrh.web.atlas;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jrh.biz.product.ProductService;
import com.jrh.dal.AtlasDao;
import com.jrh.dal.AtlasDetailDao;
import com.jrh.dal.CategoryDao;
import com.jrh.dal.LabelDao;
import com.jrh.domain.entity.Atlas;
import com.jrh.domain.entity.AtlasDetail;
import com.jrh.domain.entity.Label;
import com.jrh.domain.entity.ProductRef;
import com.jrh.web.form.FormCopier;

/**
 * 美图集
 * 
 * @author jasen.hsu.cn@gmail.com 2012-7-26 上午9:35:12
 */
@Controller
@RequestMapping("/atlas")
public class AtlasController {

    @Autowired
    private AtlasDao             atlasDao;

    @Autowired
    private AtlasDetailDao       atlasDetailDao;

    @Autowired
    private LabelDao             labelDao;

    @Autowired
    private CategoryDao          categoryDao;

    @Autowired
    private ProductService       productService;

    private static final Integer DEFAULT_ATLAS_PAGESIZE = 8;

    /**
     * 首页
     * 
     * @param model
     * @return
     */
    @RequestMapping("")
    public String index(AtlasForm atlasForm, Model model) {
        Atlas atlas = FormCopier.convert(atlasForm, null);
        model.addAttribute("styles", labelDao.listChildLabel(Label.LABEL_STYLE_KEY));
        model.addAttribute("categorys", categoryDao.listParentCategory());
        model.addAttribute("_data_", atlasDao.list(atlas, atlasDao.getStartIndex(atlasForm.getPageIndex(),
                                                                                 DEFAULT_ATLAS_PAGESIZE),
                                                   DEFAULT_ATLAS_PAGESIZE));
        model.addAttribute("form", atlasForm);
        return "atlas/list";
    }

    /**
     * 风格首页
     * 
     * @param model
     * @return
     */
    @RequestMapping("/style/{id}")
    public String listByStyle(@PathVariable Long id, Model model) {
        AtlasForm atlasForm = new AtlasForm();
        atlasForm.setStyle(id);
        return this.index(atlasForm, model);
    }

    /**
     * 类目首页
     * 
     * @param model
     * @return
     */
    @RequestMapping("/category/{id}")
    public String listByCategory(@PathVariable Long id, Model model) {
        AtlasForm atlasForm = new AtlasForm();
        atlasForm.setCategory(id);
        return this.index(atlasForm, model);
    }

    /**
     * 详情
     * 
     * @param atlasId
     * @param model
     * @return
     */
    @RequestMapping("/{atlasId}")
    public String detail(@PathVariable Long atlasId, AtlasForm atlasForm, Model model) {
        Atlas atlas = atlasDao.load(atlasId);
        Atlas previous = null;
        Atlas next = null;
        List<AtlasDetail> atlasDetails = atlasDetailDao.listByAtlasId(atlasId);
        if (atlas != null) {
            previous = atlasDao.loadPrevious(atlas.getId(), atlasForm.getStyle(), atlasForm.getCategory());
            next = atlasDao.loadNext(atlas.getId(), atlasForm.getStyle(), atlasForm.getCategory());
        }
        // 美图
        model.addAttribute("atlas", atlas);
        model.addAttribute("previous", previous);
        model.addAttribute("next", next);
        // 详情
        model.addAttribute("atlasDetails", atlasDetails);
        model.addAttribute("products",
                           productService.listProductByProductRef(atlasId, ProductRef.PRODUCT_REF_TYPE_ATLAS));
        return "atlas/detail";
    }

}
